package com.zhy.baselibrary.adapter;

import android.graphics.Rect;
import android.view.View;
import androidx.recyclerview.widget.RecyclerView;

// 自定义 ItemDecoration 类
public class HorizontalSpaceItemDecoration extends RecyclerView.ItemDecoration {

    private final int horizontalSpaceWidth;
    private final int count;

    // 构造函数，接收横向间距值
    public HorizontalSpaceItemDecoration(int horizontalSpaceWidth,int count) {
        this.horizontalSpaceWidth = horizontalSpaceWidth;
        this.count = count;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                               RecyclerView.State state) {

        // 获取当前 item 在 RecyclerView 中的位置
        int position = parent.getChildAdapterPosition(view);
        // 获取 RecyclerView 的适配器的 item 数量
        int itemCount = state.getItemCount();

        // 如果不是最后一个 item，则为其右侧添加横向间距
        if ((position+1) % count != 0){
            outRect.right = horizontalSpaceWidth;
        }

    }
}